'use strict'

import { app, BrowserWindow, ipcMain, Tray, Menu, globalShortcut } from 'electron'
// import { app, BrowserWindow, ipcMain } from 'electron'
/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
  global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
let IS_LOGIN = true
const winURL = process.env.NODE_ENV === 'development'
  ? `http://localhost:9080`
  : `file://${__dirname}/index.html`

function createWindow () {
  /**
   * Initial window options
   */
  mainWindow = new BrowserWindow({
    height: 603,
    minWidth: 914,
    minHeight: 603,
    useContentSize: true,
    width: process.env.NODE_ENV === 'development' ? 1300 + 294 : 1000,
    frame: false,
    transparent: true,
    show: false
  })

  mainWindow.loadURL(winURL)
  // mainWindow.loadURL(winURL + '#/login')
  // mainWindow.webContents.openDevTools()
  mainWindow.on('closed', () => {
    mainWindow = null
  })
}

let loginWindow
function createLogin () {
  /**
   * Initial window options
   */
  loginWindow = new BrowserWindow({
    height: 344,
    useContentSize: false,
    width: process.env.NODE_ENV === 'development' ? 444 + 294 : 444,
    frame: false,
    transparent: true,
    maximizable: false,
    alwaysOnTop: true
  })

  loginWindow.loadURL(winURL + '#/login')
  // loginWindow.webContents.openDevTools()
  loginWindow.on('closed', () => {
    loginWindow = null
  })
}

app.on('ready', function () {
  globalShortcut.register('CommandOrControl+Alt+Shift+L', () => { // 打开聊天主窗口
    if (IS_LOGIN) {
      console.log(mainWindow.isVisible())
      if (mainWindow.isVisible()) {
        mainWindow.hide()
      } else {
        mainWindow.setOpacity(0)
        mainWindow.show()
        setTimeout(function () {
          mainWindow.setOpacity(1)
        }, 100)
      }
    }
  })
  // globalShortcut.register('CommandOrControl+Shift+F12', () => { // 打开聊天主窗口
  //   mainWindow.webContents.openDevTools()
  // })
  createWindow()
  const tray = new Tray(__static + '/icon.ico') // 创建任务栏图标
  const trayContextMenu = Menu.buildFromTemplate([
    {
      label: '打开',
      click: function () {
        if (mainWindow) {
          mainWindow.setOpacity(0)
          mainWindow.show()
          setTimeout(function () {
            mainWindow.setOpacity(1)
          }, 100)
        }
      }
    },
    {
      label: '退出',
      click: function () {
        app.quit()
      }
    }
  ])
  tray.on('right-click', () => {
    tray.popUpContextMenu(trayContextMenu)
  })
  tray.setToolTip('海草客服') // 图标提示文本
  tray.on('click', () => { // 突变点击事件
    if (mainWindow) {
      mainWindow.setOpacity(0)
      mainWindow.show()
      setTimeout(function () {
        mainWindow.setOpacity(1)
      }, 100)
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (mainWindow === null) {
    createWindow()
  }
})

ipcMain.on('login_to_main', function () { // 登录成功跳转主窗口
  IS_LOGIN = true // 设置登录状态为true
  loginWindow.close()
  mainWindow.show()
  mainWindow.loadURL(winURL)
})

ipcMain.on('close_main_window', function () { // 隐藏主窗口
  mainWindow.hide()
})

ipcMain.on('show_main_window', function () { // 显示主窗口
  if (IS_LOGIN) { // 登录状态为true才能打开主窗口
    mainWindow.show()
  }
})

ipcMain.on('min_main_window', function () { // 最小化主窗口
  mainWindow.minimize()
})

let isMaximizeds = 0
ipcMain.on('max_main_window', function () { // 最大化主窗口
  if (isMaximizeds) {
    mainWindow.unmaximize()
    isMaximizeds = 0
  } else {
    mainWindow.maximize()
    isMaximizeds = 1
  }
})

ipcMain.on('open_login_window', function () { // 打开登录窗口
  IS_LOGIN = false // 设置登录状态为flase
  mainWindow.hide() // 隐藏主窗口
  createLogin() // 创建登录窗口
})

ipcMain.on('close_login_window', function (event, data) { // 关闭登录窗口
  app.quit()
})

/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */
